package com.czq.gulimail.gulimailproduct.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.common.utils.PageUtils;
import com.czq.common.utils.Query;
import com.czq.gulimail.gulimailproduct.dao.BrandDao;
import com.czq.gulimail.gulimailproduct.entity.BrandEntity;
import com.czq.gulimail.gulimailproduct.service.BrandService;
import com.czq.gulimail.gulimailproduct.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            wrapper.like("name",key).or().eq("brand_id",key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updateCategoryAndRelations(BrandEntity brand) {
        this.updateById(brand);
        categoryBrandRelationService.updateByBrand(brand.getBrandId(),brand.getName());
    }

    @Transactional
    @Override
    public void removeCategoryAndRelations(Long[] brandIds) {
        this.removeByIds(Arrays.asList(brandIds));
        categoryBrandRelationService.deleteBatchByBrandId(brandIds);
    }

}
